package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.FlowPageAnalysis;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.param.*;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.coupon.comment.service.CouponUserService;
import com.yami.shop.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * 商品分析接口
 * @author
 */
@Api(tags = "商品分析接口")
@RestController
@RequestMapping("/platform/prodAnalysis")
@AllArgsConstructor
public class ProdAnalysisController {

    private final ProductService productService;
    private final OrderItemService orderItemService;
    private final CouponUserService couponUserService;
    private final FlowPageAnalysisService flowPageAnalysisService;


    /**
     * 获取商品概况
     */
    @GetMapping("/getProdSurvey")
    @ApiOperation(value = "获取商品概况", notes = "获取商品概况")
    public ResponseEntity<ProdAnalysisParam> getProdSurvey(ProdAnalysisSurveyParam param) {
        ProdAnalysisParam analysis = productService.getProdSurvey(param);
        return ResponseEntity.ok(analysis);
    }

    /**
     * 获取商品趋势分析
     */
    @GetMapping("/getProdTrendAnalysis")
    @ApiOperation(value = "获取商品趋势分析", notes = "获取商品趋势分析")
    public ResponseEntity<List<ProdAnalysisDataParam>> getProdTrendAnalysis(ProdAnalysisSurveyParam param) {
        List<ProdAnalysisDataParam> trendAnalysisa = productService.getProdTrendAnalysis(param);
        return ResponseEntity.ok(trendAnalysisa);
    }

    /**
     * 支付金额TOP
     * 访客数TOP
     */
    @GetMapping("/getPayAmountTop")
    @ApiOperation(value = "支付金额TOP、访客数TOP", notes = "支付金额TOP、访客数TOP")
    public ResponseEntity<VisitorAndPayTopParam> getPayAmountTop(PageParam<OrderItem> page, ProdAnalysisSurveyParam param) {
        IPage<PayTopParam> pages = orderItemService.getPayAmounts(page, param, I18nMessage.getDbLang());
        VisitorAndPayTopParam res = new VisitorAndPayTopParam();
        List<PayTopParam> payTopParams = pages.getRecords();
        payTopParams.sort(Comparator.comparing(PayTopParam::getPayAmount).reversed());
        res.setPayAmounts(payTopParams);
        // 访客TOP
        IPage<FlowPageAnalysis> pageVisitor = new PageParam<>();
        pageVisitor.setSize(page.getSize());
        pageVisitor.setCurrent(page.getCurrent());
        pageVisitor.setPages(page.getPages());
        param.setShopId(null);
        IPage<VisitorTopParam> visitorTops = flowPageAnalysisService.getVisitorTop(pageVisitor,param);
        List<VisitorTopParam> records = visitorTops.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return ResponseEntity.ok(res);
        }
        for (VisitorTopParam record : records) {
            // 在筛选时间范围内，某件商品，有几个人支付
           Integer num = orderItemService.getPayPersonNumByParam(record.getProdId(),param.getShopId(),param.getStartTime(),param.getEndTime());
           record.setVisitorToPayRate(divAverage(num,record.getVisitorNum(),4));
        }
        records.sort(Comparator.comparing(VisitorTopParam::getVisitorNum).reversed());
        res.setVisitors(records);
        return ResponseEntity.ok(res);
    }


    /**
     * 获取商品效果分析数据
     */
    @GetMapping("/getProdEffect")
    @ApiOperation(value = "获取商品效果分析数据", notes = "获取商品效果分析数据")
    public ResponseEntity<IPage<ProdEffectRespParam>> getProdEffect(PageParam<Product> page,ProdEffectParam param) {
        if (Objects.equals(1,param.getGroup())) {
            IPage<ProdEffectRespParam> map = new PageParam<>();
            map.setCurrent(page.getCurrent());
            map.setSize(page.getSize());
            map.setPages(page.getPages());
            map.setRecords(new ArrayList<>());
            return ResponseEntity.ok(map);
        }
        IPage<ProdEffectRespParam> resPage = productService.getProdEffect(page,param);
        return ResponseEntity.ok(resPage);
    }
    /**
     * 单个商品的趋势分析
     */
    @GetMapping("/getSingleProdTrend")
    @ApiOperation(value = "获取商品效果分析数据", notes = "获取商品效果分析数据")
    public ResponseEntity<List<ProdSingleTrendParam>> getSingleProdTrend(Long prodId,ProdEffectParam param) {
        List<ProdSingleTrendParam> resList = productService.getSingleProdTrend(prodId,param);
        return ResponseEntity.ok(resList);
    }

    /**
     * 卡券分析，卡券昨日关键指标
     */
    @ApiOperation(value = "卡券分析，卡券昨日关键指标", notes = "卡券分析，卡券昨日关键指标")
    @GetMapping("/getCouponAnalysis")
    public ResponseEntity<List<CouponAnalysisParam>> getCouponAnalysis(ProdEffectParam param) {
        // 统计近7天的数据
        List<CouponAnalysisParam> resList = couponUserService.getCouponAnalysis(param);
        return ResponseEntity.ok(resList);
    }

    private Double divAverage(Double a, Integer b, Integer scale) {
        if (Objects.isNull(b) || b == 0 || Objects.isNull(a)) {
            return 0.0;
        }
        return Arith.div(a, b, scale);
    }
    private Double divAverage(Integer a, Integer b, Integer scale) {
        if (Objects.isNull(b) || b == 0 || Objects.isNull(a)) {
            return 0.0;
        }
        return Arith.div(a, b, scale);
    }
}
